package 网络编程.UDP发送数据;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

//还是要等待客户端的连接
public class UdpserverDemo01 {
    public static void main(String[] args) throws Exception{
        //开放端口
        DatagramSocket socket = new DatagramSocket(9990);
        System.out.println("服务端启动成功");
        //接收数据包
        byte[] buffer = new byte[1024];

        DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length);
        socket.receive(packet);//阻塞接收
        System.out.println("接收数据包成功");
        System.out.println(packet.getAddress().getHostAddress());
        System.out.println(new String(packet.getData(),0,packet.getLength()));
        //关闭连接
        socket.close();
    }
}
